package com.example.carrierapp.ui.main.dispatch.add;

import android.content.Context;

import com.example.carrierapp.base.BasePresenter;
import com.example.carrierapp.net.ApiMiddleHelper;
import com.example.carrierapp.net.listener.DataCallBackListener;
import com.example.carrierapp.util.LogUtil;
import com.example.carrierapp.viewmodel.itemmodel.ItemSearchModel;
import com.google.gson.JsonObject;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Flowable;

public class DispatchAddPresenter extends BasePresenter<DispatchAddContract.View> implements DispatchAddContract.Presenter {
    private Context mContext;

    public DispatchAddPresenter(Context context) {
        mContext = context;
    }

    @Override
    public void start() {
    }

    @Override
    public void addDispatch(ItemSearchModel car, ItemSearchModel driver, String num, String remark, int shipmentId) {
        Map<String, String> biddingMap = new HashMap<>();
        biddingMap.put("shipmentId", shipmentId + "");
        biddingMap.put("driverId", driver.getId()+"");
        biddingMap.put("carId", car.getId()+"");
        biddingMap.put("name", driver.getName());
        biddingMap.put("phone", driver.getPhone());
        biddingMap.put("carNumber", car.getName());
        biddingMap.put("quantity", num + "");
        biddingMap.put("remarks", remark);
        Flowable<JsonObject> flowable = apiStrores.addDispatch(biddingMap);

        ApiMiddleHelper.getInstance().toNetIo2Main(mContext, flowable, new DataCallBackListener() {
            @Override
            public void onSuccess(String json) {
                LogUtil.d("addDispatch-onSuccess-" + json);
                if (getView() != null) {
                    getView().addDispatchSuccess(json);
                }
            }

            @Override
            public void onFailure(String msg) {
                LogUtil.d("addDispatch-onFailure-" + msg);
                if (getView() != null) {
                    getView().addDispatchFailde(msg);
                }
            }
        });
    }
}
